import api from "../index";
import type {
  FormCartParams,
  CardFormData,
  GoodsForm,
  PurchaseGoodsPayData,
} from ".";
import type { ResponseData,pageParam,RequestFromType } from '../type'


export interface GoodsLayoutFormParam {
  proId: string; //商品id
  subType: 0 | 1 | 2; //提交类型   金额校验传0 单笔订单提交、预览修改时传1  在购物车中修改、提交表单传2 在预览订单中有多笔订单的情况下修改表单传2
  skuId: string; //商品skuid
  formId: string | number; //表单id
  proPrice: string | number; //商品金额
  proSize: string | number; //商品数量
  list: FormCartParams["list"]; //表单数据
}
export interface SystemLayoutFormParam extends Partial<{
	fromType: RequestFromType,
	payId : number
	payword ?: string
}> {
  subType: 1;
  formId: string | number; //表单id
  list: FormCartParams["list"]; //表单数据
}

// 自定义表单提交、修改、校验，平台自定义表单则为支付
export const postSubmitLayoutForm = (
  data: GoodsLayoutFormParam,
  isLoading = true
) => {
  
  return api.post<ResponseData<CardFormData>>("customer/api/form/sub", {
    data,
    header: {
      'Content-Type': 'application/json'
    },
    custom: {
      isLoading,
      parseJson: true,
    },
  });
}
// 获取平台自定义表单数据
export const getSystemLayoutFormJson = (data: { formId: string | number }) =>
  api.get<ResponseData<GoodsForm>>("customer/api/sysform/get", {
    data,
    custom: {
      isLoading: true,
    },
  });

// 平台自定义表单提交预览，获取支付金额，为0则无需支付
export const postSystemLayoutFormBrowse = (data: SystemLayoutFormParam) =>
  api.post<ResponseData<CardFormData>>("customer/api/sysform/browse", {
    data,
    header: {
      'Content-Type': 'application/json'
    },
    custom: {
      isLoading: true,
    },
  });
// 平台自定义表单提交
export const postSubmitSystemLayoutForm = (
  data: SystemLayoutFormParam,
  isLoading = true
) =>
  api.post<PurchaseGoodsPayData>("customer/api/sysform/sub", {
    data,
    header: {
      'Content-Type': 'application/json'
    },
    custom: {
      isLoading
    },
  });
// 自定义表单支付结果查询
export const getLayoutFormPayResult = (id: string) =>
  api.get(`order/api/main/payCode/${id}`);


export interface LayoutFormOrderItem {
  id:number
  orderId: string,
  userId: string,
  userNickname: string,
  userName: string,
  payMoney: number,
  formTitle: string,
  payStatus: number,
  createTime: string,
  replyStatus: number,
  formJson: string,
  replyInfo: string
}
// 自定义表单订单列表
export const getLayoutFormOrderList = (data: pageParam) =>
    api.get<ResponseData<LayoutFormOrderItem,true>>('customer/api/sysform/order/list', {
        data,
        custom: {
            isLoading: true
        }
    })
// 自定义表单订单详情
export const getLayoutFormOrderInfo = (data: {id:string|number}) =>
    api.get<ResponseData<LayoutFormOrderItem>>('customer/api/sysform/order/get', {
        data,
        custom: {
            isLoading: true
        }
    })
  
  // 获取-自定义表单是否开启
  export const getLayoutFormSetting = () =>
  api.get<ResponseData<'Y'|'N'>>("system/basics/form/get", {
    custom: {
      auth: false,
    },
  });